package shop.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import shop.entity.ResponseMessage;
import user.dto.UserDto;
import user.params.TypeUpdateDto;
import user.params.UserAddParam;
import user.params.UserLoginParam;
import user.params.UserSearchParam;
import user.service.UserServiceI;

import java.util.HashMap;
import java.util.List;

import static shop.entity.ResponseMessage.success;

@RestController
@RequestMapping("/userApi")
public class UserController {
    @DubboReference(version = "1.0.0")
    private UserServiceI userServiceI;
    /**
     * 获得所有用户
     * */
    @GetMapping("/users")
    public String getAll() {
        return userServiceI.getAllUsers().toString();
    }
    /**
     * 添加用户
     * */
    @PostMapping("/user")
    public ResponseMessage<UserDto> addUser(@RequestBody UserAddParam userAddParam) {
        UserDto userDto = userServiceI.addUser(userAddParam);
        return ResponseMessage.success(userDto);
    }
    /**
     * 用户登录
    * */
    @PostMapping("/login")
    public ResponseEntity<?> test(@RequestBody UserLoginParam userLoginParam) {
        UserDto userDto = userServiceI.login(userLoginParam);
        if (userDto == null)
            return ResponseEntity.notFound().build();
        /**
         * 改为发送token加数据会更好
         */
        HashMap<String, Object> response = new HashMap<>();
        response.put("user", userDto);
        /******
         * 创建token
         * 。。。。。。。。。。。。。。。。。。。
         * response.put("token", token);
         */
        String token = userDto.getUsername();
        response.put("token", token);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/SearchUser")
    public String SearchUser(@RequestBody UserSearchParam userSearchParam) {
        return "test";
    }

    @PostMapping("/password")
    public String updatePassword(@RequestBody UserLoginParam userLoginParam) {
        return userServiceI.updatePassword(userLoginParam.getUsername(), userLoginParam.getPassword());
    }
    /**
     * 获得用户类型
     */

    @GetMapping("/UserType")
    public String getUserType(@RequestParam("userId") int userId) {
        return  userServiceI.getUserType(userId);
    }
    /**
     * 更新用户类型
    * */
    //更新用户类型
    @PutMapping("/info")
    public String updateUserType(@RequestBody TypeUpdateDto typeUpdateDto) {
        return userServiceI.updateUserType(typeUpdateDto);
    }
    /**
     * 删除用户
     * */
    @DeleteMapping("/user")
    public String delUser(int userId) {
        userServiceI.delUser(userId);
        return "success";
    }
}
